SpringCloud各服务之间访问

一、创建服务注册中心以及服务。
1、创建注册中心eureka-server。
2、创建服务eureka-server-user且添加获取用户名称的get方法。

package com.example.serveruser.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ServerUserController {
    @GetMapping(value = "/getUserName")
    public String getUserName() {
        return "张三琪";
    }
}

二、Feign
1、创建服务eureka-server-feign,初始化模块时选择EurekaServer以及OpenFile或者在pom中添加对应的包。

<dependency>
  <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2、在启动类中添加声明注解@EnableFeignClients。
3、添加接口ServerUserFeign且将feign指向eureka-server-user服务以及添加eureka-server-user中获取用户姓名的方法。
package com.example.serverfeign.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "server-user")
public interface ServerUserFeign {
    @GetMapping(value = "/getUserName")
    String getUserName();
}

4、添加controller类,引入ServerUserFeign 。

package com.example.serverfeign.web;
import com.example.serverfeign.feign.ServerUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ServerFeignController {
    @Autowired
    ServerUserFeign serverUserFeign;
    @GetMapping(value = "/getServerUserName")
    public String getServerUserName() {
        return serverUserFeign.getUserName();
    }
}

5、通过RunDashboard启动项目,访问eureka-server-feign中的getServerUserName即可调用eureka-server-user的getUserName获取到返回结果“张三琪”。

三、LoadBalancerClient
1、创建服务server-balancer,初始化模块时选择EurekaServer以及CloudLoadBanlancer或者在pom中添加对应的包。

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2、启动类中添加注解声明@EnableDiscoveryClient以及RestTemplate的初始化方法。

package com.example.serverbalancer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class ServerBalancerApplication {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(ServerBalancerApplication.class, args);
    }
}

3、增加controller类,通过loadBalancerClient以及restTemplate访问server-user的getUserName方法。

package com.example.serverbalancer.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ServerBalancerController {
    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    RestTemplate restTemplate;
    @GetMapping("/getServerUserName")
    public String getServerUserName() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("server-user");
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/getUserName";
        return restTemplate.getForObject(url, String.class);
    }
}

四、Ribbon
1、创建服务server-ribbon,初始化模块时选择EurekaServer以及Ribbon或者在pom中添加对应的包。

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

2、启动类中添加注解声明@EnableDiscoveryClient以及RestTemplate的初始化方法。

package com.example.serverribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class ServerRibbonApplication {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(ServerRibbonApplication.class, args);
    }
}

3、增加controller类,通过restTemplate访问server-user的getUserName方法。

package com.example.serverribbon.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ServerRibbonController {
    @Autowired
    RestTemplate restTemplate;
    @GetMapping("/getServerUserName")
    public String getServerUserName() {
        return restTemplate.getForObject("http://server-user/getUserName", String.class);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值