springClude简单配置

引言:先创建一个父工程spring-cloud-parent,包括四个个模块,一个提供者spring-cloud-provider,一个消费者spring-cloud-consumer,一个注册中心spring-cloud-eureka,一个存储公共资源的模块spring-cloud-common

1、父工程spring-cloud-parent加载的依赖

<dependencyManagement>
    <dependencies>
      <!-- 导入SpringCloud需要使用的依赖信息 -->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Greenwich.SR2</version>
        <type>pom</type>
        <!-- import依赖范围表示将spring-cloud-dependencies包中的依赖信息导入 -->
        <scope>import</scope>
      </dependency>
      <!-- 导入SpringBoot需要使用的依赖信息 -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.1.6.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

2、spring-cloud-provider模块需要加载的资源

<parent>
        <artifactId>spring-cloud-study-parent</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
  </parent>

<dependencies>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>spring-cloud-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
 </dependencies>

2.1、静态资源配置文件application.yml:

server:
  port: 1000
  
spring:
  application:
    name: atguigu-provider    # 指定当前微服务名称,以便将来通过微服务名称调用当前微服务时能够找到

eureka:
  client:
    service-url:    # 配置当前微服务作为Eureka客户端访问Eureka服务器端时使用的地址
      defaultZone: http://localhost:5000/eureka

2.2、创建一个controller类,类名为EmployeeHandler,写一个测试方法:

@RequestMapping("/provider/get/employee/remote")
	public Employee getEmployeeRemote() {

		return new Employee(555, "tom555", 555.55);
	}

3、spring-cloud-consumer模块需要加载的资源

 <parent>
        <artifactId>spring-cloud-study-parent</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
   </parent>
 <dependencys>
 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>spring-cloud-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
 </dependencys>

3.1、静态资源配置文件application.yml:

server:
  port: 7000
spring:
  application:
    name: atguigu-consumer
eureka:
  client:
    service-url:
      defaultZone: http://localhost:5000/eureka/

3.2、创建一个controller类,类名为HumanResourceHandler,写一个测试方法:

	@Autowired
	private RestTemplate restTemplate;
	
	@RequestMapping("/consumer/get/employee")
	public Employee getEmployeeRemote() {
		
		// 1.声明远程微服务的主机地址加端口号
		//String host = "http://localhost:1000";


		// 1.加入wureka后,使用微服务名称替代 IP 地址+端口号
		String host = "http://atguigu-provider";
		
		// 2.声明具体要调用的功能的URL地址
		String url =  "/provider/get/employee/remote";
		
		// 3.通过RestTemplate调用远程微服务
		return restTemplate.getForObject(host + url, Employee.class);
	}

3.3、创建一个配置类AtguiguSpringCloudConfig,加载RestTemplate:

@Configuration
public class AtguiguSpringCloudConfig {
	
	@Bean
	@LoadBalanced
	public RestTemplate getRestTemplate() {
		return new RestTemplate();
	}

}

4、spring-cloud-eureka模块需要加载的资源

<parent>
    <artifactId>spring-cloud-study-parent</artifactId>
    <groupId>org.example</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>


<dependencys>
 <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-netflix-eureka-client</artifactId>
    </dependency>
</dependencys>

4.1、静态资源配置文件application.yml:

server:
  port: 5000

eureka:
  instance:
    hostname: localhost         # 配置当前Eureka服务的主机地址
  client:
    register-with-eureka: false # 当前服务本身就是注册中心,不必“自己注册自己”
    fetch-registry: false       # 当前服务本身就是注册中心,不必“从注册中心取回信息”
    service-url: # 客户端(指consumer、provider)访问当前注册中心时使用的地址
      defaultZone: http://${eureka.instance.hostname}/${server.port}/eureka

4.2、启动类要加@EnableEurekaServer注解:

/ 启用Eureka服务器端功能
@EnableEurekaServer
@SpringBootApplication
public class AtguiguMainType {
	
	public static void main(String[] args) {

		SpringApplication.run(AtguiguMainType.class, args);
	}

}

5、spring-cloud-common模块需要加载的资源:

 <parent>
        <artifactId>spring-cloud-study-parent</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
 <dependencys>
 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
 </dependencys>

5.1、创建一个实体类Employee:

public class Employee {
	
	private Integer empId;
	private String empName;
	private Double empSalary;
	
	public Employee() {
		
	}

	public Employee(Integer empId, String empName, Double empSalary) {
		super();
		this.empId = empId;
		this.empName = empName;
		this.empSalary = empSalary;
	}

	@Override
	public String toString() {
		return "Employee [empId=" + empId + ", empName=" + empName + ", empSalary=" + empSalary + "]";
	}

	public Integer getEmpId() {
		return empId;
	}

	public void setEmpId(Integer empId) {
		this.empId = empId;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public Double getEmpSalary() {
		return empSalary;
	}

	public void setEmpSalary(Double empSalary) {
		this.empSalary = empSalary;
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值