srpingcolud简单的小工程

来源:互联网 发布:vb.net sleep 编辑:程序博客网 时间:2024/05/22 04:26

Ø  微服务架构搭建步骤1——创建服务注册中心

Ø  微服务架构搭建步骤2——创建服务提供方

Ø  微服务架构搭建步骤3——创建服务消费方

 

注册中心springcolud_test

 

Pom文件不需加其他配置

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">   <modelVersion>4.0.0</modelVersion>   <groupId>com.idcard.interview</groupId>   <artifactId>springcolud_test</artifactId>   <version>0.0.1-SNAPSHOT</version>   <packaging>jar</packaging>   <name>springcolud_test</name>   <description>Demo project for Spring Boot</description>   <parent>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-parent</artifactId>      <version>1.5.6.RELEASE</version>      <relativePath/> <!-- lookup parent from repository -->   </parent>   <properties>      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>      <java.version>1.8</java.version>   </properties>   <dependencies    <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-web</artifactId>      </dependency>      <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-test</artifactId>         <scope>test</scope>      </dependency>      <!--spring cloud center pom-->      <dependency>         <groupId>org.springframework.cloud</groupId>         <artifactId>spring-cloud-starter-eureka-server</artifactId>      </dependency>   </dependencies>   <!--spring cloud center management plugin-->   <dependencyManagement>      <dependencies>         <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-dependencies</artifactId>            <version>Dalston.SR1</version>            <type>pom</type>            <scope>import</scope>         </dependency>      </dependencies>   </dependencyManagement>   <build>      <plugins>         <plugin>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-maven-plugin</artifactId>         </plugin>      </plugins>   </build></project>

 

 

注册中心配置

 

#服务注册中心
spring.application.name=springboot_test
server.port=1111

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

 

在入口程序中添加注解

 

@SpringBootApplication
@EnableEurekaServer
public class SpringcoludTestApplication{

  
public static void main(String[]args) {
     
SpringApplication.run(SpringcoludTestApplication.class, args);
  
}
}

 

创建服务提供方springboot_service

可以加其他pom,但是相应配置必须加入,如不加入会出现错误

<!--服务提供方的依赖包-->

 <dependencies>
   
<dependency>
       
<groupId>org.springframework.cloud</groupId>
       
<artifactId>spring-cloud-starter-eureka</artifactId>
   
</dependency>
</
dependencies>

<
dependencyManagement>
   
<dependencies>
       
<dependency>
           
<groupId>org.springframework.cloud</groupId>
           
<artifactId>spring-cloud-dependencies</artifactId>
           
<version>Brixton.RELEASE</version>
           
<type>pom</type>
           
<scope>import</scope>
       
</dependency>
   
</dependencies>
</
dependencyManagement>

配置

#spring cloud配置
spring.application.name=springboot_service
server.port=2222
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

 

 

在入口程序中添加注解

 

@SpringBootApplication
@EnableDiscoveryClient
public class SpringbootServiceApplication{

  
public static void main(String[]args) {
     
SpringApplication.run(SpringbootServiceApplication.class, args);
  
}
}

 

springboot_service提供方的controller

 

@RestController    public class ComputerController {        @RequestMapping("/plus/{a}/{b}")        public Integer plus(@PathVariable Integer a, @PathVariable Integer b){        return a+b;        }

 

 

springboot _ consumer消费方

注入依赖

<dependencies>

<dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-feign</artifactId>   </dependency<dependency>   <groupId>org.springframework.cloud</groupId>   <artifactId>spring-cloud-starter-eureka</artifactId></dependency></dependencies><dependencyManagement>   <dependencies>      <dependency>         <groupId>org.springframework.cloud</groupId>         <artifactId>spring-cloud-dependencies</artifactId>         <version>Brixton.RELEASE</version>         <type>pom</type>         <scope>import</scope>      </dependency>   </dependencies></dependencyManagement>

配置

sspring.application.name=springboot_consumerserver.port=3333eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

 

在入口程序中添加注解并注入RestTemplate到bean容器

 

@SpringBootApplication
@EnableDiscoveryClient
public class SpringbootConsumerApplication{
  
@Bean
  
public RestTemplaterestTemplate() {
     
return new RestTemplate();
  
}


  
public static void main(String[]args) {
     
SpringApplication.run(SpringbootConsumerApplication.class, args);
 
 }

 

springboot_consumer controller中注入

 

@Autowiredprivate LoadBalancerClient lbc;@Autowiredprivate RestTemplate template;@RequestMapping("/comsumer/{a}/{b}")public String computePlus(@PathVariable Integer a, @PathVariable Integer b){    //发现服务    ServiceInstance choose = lbc.choose("springboot_service");    String url = "http://" + choose.getHost() + ":" + choose.getPort() + "/plus/"+a+"/"+b;    Integer result = template.getForObject(url, Integer.class);    return a+"+"+b+"="+result;}

 

 访问路径

http://localhost:3333/comsumer/1/2s

原创粉丝点击