spring 配置

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

引言:先创建一个父工程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;
	}

}

知秋君
上一篇 2024-08-09 13:12
下一篇 2024-07-04 08:02

相关推荐