码迷,mamicode.com
首页 > 编程语言 > 详细

使用eclipse一步一步创建SpringCloud项目(二)—— 使用feign和ribbon调用微服务

时间:2019-09-15 19:56:43      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:hot   ret   turn   main   执行   resource   style   utf-8   vat   

一、创建服务提供者service-hi

1、创建子项目的方法类似eureka,略

技术图片

 

2、修改pom文件,主要是添加jar包

<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>
    <parent>
        <groupId>com.landcode</groupId>
        <artifactId>land</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <artifactId>land-service-hi</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.landcode</groupId>
            <artifactId>land-common</artifactId>
            <version>1.0.0-SNAPSHOT</version>        
        </dependency>
        
     <!-- 引入eureka jar包--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <build> <finalName>land-service-hi</finalName> // jar包名 <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <!-- 打包成可执行jar包 --> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <encoding>utf-8</encoding> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>

 

3、resource目录下添加配置文件application.yml

server:
  port: 8855
  
spring:
  application:
    name: service-hi

eureka:
  instance:
    prefer-ip-address: true   #开启显示IP地址
    instance-id: ${spring.cloud.client.ip-address}:${server.port}   #eureka页面显示IP地址:端口号
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

 

4、新建启动类,注意加上@EnableEurekaClient 注解,到eureka 注册服务

@SpringBootApplication
@EnableEurekaClient
public class HiApplication {

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

 

5、新建一个测试controller,方便服务调用方测试。测试功能中会返回服务方的端口号,方便做负载均衡测试

@RestController
public class HiController {
    
    @Value("${server.port}")
    private String port;
    
    @RequestMapping(value = "/hi")
    public String hi() {
        return "hi " + port;  // 返回端口号,方便负载均衡功能测试
    }
}

 

二、创建服务消费方service-consumer

1、新建子项目service-consumer 作为服务调用方,方法类似eureka(略),修改pom文件,添加eureka、openfeign的jar包

<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>
    <parent>
        <groupId>com.landcode</groupId>
        <artifactId>land</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <artifactId>land-service-consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.landcode</groupId>
            <artifactId>land-common</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

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

    <build>
        <finalName>land-service-consumer</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <!-- 打包成可执行jar包 -->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>utf-8</encoding>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

2、resource目录添加项目配置文件application.yml

server:
  port: 8866
  
spring:
  application:
    name: service-consumer

eureka:
  instance:
    prefer-ip-address: true   #开启显示IP地址
    instance-id: ${spring.cloud.client.ip-address}:${server.port}   #eureka页面显示IP地址:端口号
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

 

3、添加项目启动类,@EnableFeignClients 表示使用feign调用服务接口,Ribbonclient 注解表示采用ribbon做负载均衡,configuration = RibbonConfig.class, RibbonConfig 是Ribbon 负载均衡机制配置类

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = { "com.landcode.service.consumer.service" })
@RibbonClient(name = "service-hi", configuration = RibbonConfig.class )
public class ConsumerApplication {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SpringApplication.run(ConsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

 

4、RibbonConfig配置类,指定使用某种负载均衡策略

@Configuration
public class RibbonConfig {
    @Resource
    IClientConfig clientConfig;

    @Bean
    public IRule ribbonRule(IClientConfig clientConfig) {
//        return new RandomRule(); //随机选择策略
        return new WeightedResponseTimeRule();
    }
}

 

5、新建service,调用服务service-hi,会自动采用ribbon的负载均衡策略

@FeignClient(value = "service-hi")
public interface ITestHi {

    @RequestMapping("/hi")
    public String testHi();
}

 

6、新建测试 controller,测试服务调用和负载均衡。这里注意:如果ITestHi还有实现类bean的话,注入时注意通过name区分

@RestController
public class TestHiController {
    
    @Autowired
    private ITestHi testHi;
    
    @RequestMapping("/consumer/hi")
    public String testHi() {
        return testHi.testHi();
    }
}

 

三、测试

12、启动eureka,启动几个service-hi服务(只需修改为不同端口号),启动service-consumer服务,打开浏览器输入http://192.168.0.3:8866/consumer/hi 调用服务消费测试接口,可以看见每次调用打印了服务提供方不同的端口号。

页面分别打印:hi 8855、hi 8856、hi 8856、hi 8857、hi 8856、hi 8855、hi 8857

 

使用eclipse一步一步创建SpringCloud项目(二)—— 使用feign和ribbon调用微服务

标签:hot   ret   turn   main   执行   resource   style   utf-8   vat   

原文地址:https://www.cnblogs.com/alan6/p/11521991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!